package by.brest.bspc.web;

/**
 * Filter to selection directories.
 *
 * @date: 08.10.13 12:18
 * @author: Natallia
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileFilter;

public class TreeFileFilter implements FileFilter {
    private static final Logger LOG = LoggerFactory.getLogger(TreeFileFilter.class);

    public static final String DOCUMENTS_PREFIX = "Documents_";

    private boolean justDirectories;

    public TreeFileFilter(boolean justDirectories) {
        this.justDirectories = justDirectories;
    }

    /**
     * If selects directories for tree then selects directories without "Documents_" prefix.
     * If selects directories for the list of the documents then selects directories with "Documents_" prefix.
     *
     * @param file file for which need to analyze the name.
     * @return true file name is accepted; false file name isn't accepted.
     */
    @Override
    public boolean accept(File file) {
        LOG.debug(">> Analysing of file: " + file);
        LOG.debug(">> Just directories? - " + justDirectories);

        if (file.isDirectory()) {
            return justDirectories ? !file.getName()
                    .contains(DOCUMENTS_PREFIX) : file.getName().contains(DOCUMENTS_PREFIX);
        }
        return false;
    }

    @Override
    public String toString() {
        return "TreeFileFilter{" +
                "justDirectories=" + justDirectories +
                '}';
    }
}
